

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/favicon.png">
  <link rel="icon" href="/blog/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="扬言">
  <meta name="keywords" content="">
  
    <meta name="description" content="我知道 React 的状态更新是异步执行的，为了性能优化，状态是批量更新的。所以你永远不能确信在调用 setState 后状态是否更新了。但是你是否可以确认 setState 调用后状态的更新顺序呢？ 比如以下情况：  相同的组件？ 不同的组件？  考虑以下按钮点击的例子：  是否有可能 a 是 false，b 是 true?  123456789101112131415class Contain">
<meta property="og:type" content="article">
<meta property="og:title" content="React 是否保持 state 更新的顺序？">
<meta property="og:url" content="http://yan_yin.gitee.io/blog/2023/07/05/react-update/index.html">
<meta property="og:site_name" content="yain&#39;blog">
<meta property="og:description" content="我知道 React 的状态更新是异步执行的，为了性能优化，状态是批量更新的。所以你永远不能确信在调用 setState 后状态是否更新了。但是你是否可以确认 setState 调用后状态的更新顺序呢？ 比如以下情况：  相同的组件？ 不同的组件？  考虑以下按钮点击的例子：  是否有可能 a 是 false，b 是 true?  123456789101112131415class Contain">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-07-05T05:29:25.000Z">
<meta property="article:modified_time" content="2023-07-11T05:42:57.101Z">
<meta property="article:author" content="扬言">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>React 是否保持 state 更新的顺序？ - yain&#39;blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"yan_yin.gitee.io","root":"/blog/","version":"1.9.5-a","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Yain&#39;Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="React 是否保持 state 更新的顺序？"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-07-05 13:29" pubdate>
          2023年7月5日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          29 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">React 是否保持 state 更新的顺序？</h1>
            
            
              <div class="markdown-body">
                
                <p>我知道 React 的状态更新是<strong>异步执行</strong>的，为了性能优化，状态是批量更新的。所以你永远不能确信在调用 <code>setState</code> 后状态是否更新了。但是你是否可以确认 <strong><code>setState</code> 调用后状态的更新顺序</strong>呢？</p>
<p>比如以下情况：</p>
<ul>
<li>相同的组件？</li>
<li>不同的组件？</li>
</ul>
<p>考虑以下按钮点击的例子：</p>
<ol>
<li>是否有可能 <strong><code>a</code> 是 <code>false</code>，<code>b</code> 是 <code>true</code></strong>?</li>
</ol>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Container</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>&#123;<br>  constructor(props) &#123;<br>    <span class="hljs-keyword">super</span>(props);<br>    <span class="hljs-keyword">this</span>.state = &#123; a: <span class="hljs-literal">false</span>, b: <span class="hljs-literal">false</span> &#125;;<br>  &#125;<br><br>  render() &#123;<br>    <span class="hljs-keyword">return</span> &lt;<span class="hljs-type">Button</span> onClick=&#123;<span class="hljs-keyword">this</span>.handleClick&#125;/&gt;<br>  &#125;<br><br>  handleClick = () =&gt; &#123;<br>    <span class="hljs-keyword">this</span>.setState(&#123; a: <span class="hljs-literal">true</span> &#125;);<br>    <span class="hljs-keyword">this</span>.setState(&#123; b: <span class="hljs-literal">true</span> &#125;);<br>  &#125;<br>&#125; <br></code></pre></td></tr></table></figure>

<ol>
<li>是否有可能 <strong><code>a</code> 是 <code>false</code>，<code>b</code> 是 <code>true</code></strong>?</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">SuperContainer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">React.Component</span> &#123;<br>  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">props</span>) &#123;<br>    <span class="hljs-variable language_">super</span>(props);<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">state</span> = &#123; <span class="hljs-attr">a</span>: <span class="hljs-literal">false</span> &#125;;<br>  &#125;<br><br>  <span class="hljs-title function_">render</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">return</span> <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Container</span> <span class="hljs-attr">setParentState</span>=<span class="hljs-string">&#123;this.setState.bind(this)&#125;/</span>&gt;</span></span><br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Container</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">React.Component</span> &#123;<br>  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">props</span>) &#123;<br>    <span class="hljs-variable language_">super</span>(props);<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">state</span> = &#123; <span class="hljs-attr">b</span>: <span class="hljs-literal">false</span> &#125;;<br>  &#125;<br><br>  <span class="hljs-title function_">render</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">return</span> <span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">&#123;this.handleClick&#125;/</span>&gt;</span></span><br>  &#125;<br><br>  handleClick = <span class="hljs-function">() =&gt;</span> &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span>.<span class="hljs-title function_">setParentState</span>(&#123; <span class="hljs-attr">a</span>: <span class="hljs-literal">true</span> &#125;);<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setState</span>(&#123; <span class="hljs-attr">b</span>: <span class="hljs-literal">true</span> &#125;);<br>  &#125;<br>&#125; <br></code></pre></td></tr></table></figure>

<p>请记住，这是我对用例的极端简化。我知道我可以改善我的代码，例如，在例 1 中同时更新两个 state 值，以及在例 2 中的第一个 <code>setState</code> 的回调中执行第二个 <code>setState</code>。但是，这不是我的问题，而且我只想知道 React 执行这些状态更新时有没有确切的方式和顺序，仅此而已。</p>
<hr>
<p>不久后，React 的作者 Dan Abramov 亲自来回答了。</p>
<blockquote>
<p>I work on React.</p>
</blockquote>
<p>开场定乾坤。很有力度的一句话。</p>
<blockquote>
<p>TLDR:</p>
</blockquote>
<p>很多英文文章中都会出现 TLDR，有时候会写成 tl_dr、tl;dr，是 Too Long; Didn’t Read 的缩写，作用就是告诉读者，这篇内容篇幅比较长，如果不想深入探讨或时间有限，可以看总结。</p>
<p>Dan Abramov 先给出了结论：</p>
<ul>
<li>同一个组件中，<code>setState</code> 是否有确定的顺序？<strong>是的</strong>。</li>
<li>不同组件中，<code>setState</code> 是否有确定的顺序？<strong>是的</strong>。</li>
</ul>
<hr>
<p>状态始终是按照特定的顺序更新的。无论你是否看到介于两个状态之间的一个中间状态，无论你是否在批处理内。</p>
<p>目前（React 16 及更早版本），默认情况下，只有 React 事件处理程序中的更新才会被批处理。有一个不稳定（unstable）的 API 来强制在事件处理程序之外进行批处理，以便在需要时处理罕见的情况。</p>
<p>在未来的版本（可能是 React 17 或更高版本）中，React 将默认批量更新所有更新，因此你不必考虑这一点。与往常一样，我们将在 <a target="_blank" rel="noopener" href="https://reactjs.org/blog/">React 博客</a>和 release note 中宣布对此的任何更改。</p>
<hr>
<p>理解这一点的关键在于，<strong>在 React 事件处理程序中，不论 <code>setState()</code> 调用了多少次，也不论 <code>setState()</code> 被多少个组件调用，它们在事件结束时只会生成一次重新渲染</strong>。这对于大型应用程序性能至关重要，因为如果 <code>Child</code> 与 <code>Parent</code> 处理 click 事件的时候都调用 <code>setState()</code>，你不希望重新渲染 <code>Child</code> 两次。</p>
<p>在这两个例子中，<code>setState()</code> 调用都发生在 <code>React</code> 事件处理程序中。因此，在事件结束时，他们总是被合并到一起（而且你看不到中间状态）。</p>
<p>更新总是<strong>按照它们发生的顺序进行浅合并（shallowly merge）</strong>。所以如果第一次更新是 <code>&#123;a: 10&#125;</code>，第二次是 <code>&#123;b: 20&#125;</code>，第三次 <code>&#123;a: 30&#125;</code>，呈现的状态将是 <code>&#123;a: 30, b: **20&#125;</code>。对 <code>state</code> 中同一个 key 的更新（例如我的例子中 <code>a</code> 的更新），最新(近)的更新总是“胜出”。**</p>
<p>当我们在批处理结束时重新呈现 UI 时，<code>this.state</code> 对象已经被更新了。所以如果你需要根据之前的状态更新状态（比如增加一个计数器），你应该使用 <code>setState(fn)</code> 的回掉函数版本来提供之前的状态，而不是读取状态 <code>this.state</code>。如果你对这个理由感到好奇，我在<a target="_blank" rel="noopener" href="https://github.com/facebook/react/issues/11527#issuecomment-360199710">这个评论</a>中深入地解释了它。</p>
<hr>
<p>在你的例子中，我们不会看到“中间状态”，因为我们在具有批处理功能的 <strong>React 事件处理程序</strong>中（因为 React “知道”什么时候退出该事件）。</p>
<p>但是，在 React 16 和更早版本中，<strong>React 事件处理程序之外还没有默认的批处理</strong>。因此，如果在你的例子中，我们吧 <code>handleClick</code> 替换为 AJAX 处理程序，那么每个 <code>setState()</code> 都会立即处理。在这种情况下，是的，你<strong>会</strong>看到一个中间状态：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js">promise.<span class="hljs-title function_">then</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>  <span class="hljs-comment">// 我们不在事件处理程序中，因此它们都会被刷新。</span><br>  <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setState</span>(&#123;<span class="hljs-attr">a</span>: <span class="hljs-literal">true</span>&#125;); <span class="hljs-comment">// 使用 &#123;a: true, b: false &#125; 重新渲染</span><br>  <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setState</span>(&#123;<span class="hljs-attr">b</span>: <span class="hljs-literal">true</span>&#125;); <span class="hljs-comment">// 使用 &#123;a: true, b: true &#125; 重新渲染</span><br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span>.<span class="hljs-title function_">setParentState</span>(); <span class="hljs-comment">// 重新渲染父组件</span><br>&#125;); <br></code></pre></td></tr></table></figure>

<p>我们认识到，根据<strong>是否处于事件处理程序中，行为是不同的</strong>，这是不方便的。这将在未来的 React 版本中进行更改，默认情况下将批量更新所有更新（并提供选择性 API 以同步刷新更改）。直到我们切换默认行为（可能在 React 17 中），有一个 API 可以用来强制批量处理：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js">promise.<span class="hljs-title function_">then</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>  <span class="hljs-comment">// 强制批量处理</span><br>  <span class="hljs-title class_">ReactDOM</span>.<span class="hljs-title function_">unstable_batchedUpdates</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setState</span>(&#123;<span class="hljs-attr">a</span>: <span class="hljs-literal">true</span>&#125;); <span class="hljs-comment">// 不重新渲染</span><br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setState</span>(&#123;<span class="hljs-attr">b</span>: <span class="hljs-literal">true</span>&#125;); <span class="hljs-comment">// 不重新渲染</span><br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span>.<span class="hljs-title function_">setParentState</span>(); <span class="hljs-comment">// 不重新渲染</span><br>  &#125;);<br>  <span class="hljs-comment">// 当我们退出 unstable_batchedUpdates函数后，重新渲染一次</span><br>&#125;); <br></code></pre></td></tr></table></figure>

<p>在 React 内部，事件处理程序都被包装在 <code>unstable_batchedUpdates</code> 内，这就是默认情况下批处理的原因。请注意，将 <code>setState</code> 封装 <code>unstable_batchedUpdates</code> 两次是不起作用的。当我们退出最外层的<code>unstable_batchedUpdates</code> 调用时，更新被刷新。</p>
<p>该 API 是“不稳定的”，因为如果默认情况下已经启用批处理，我们将删除它。但是，在 React 小版本中我们不会删除它，所以在 React 17 之前，如果你需要在 React 事件处理程序之外的某些情况下强制批处理，你可以安全地依赖它。</p>
<hr>
<p>总之，这是一个令人困惑的话题，因为 React 默认只在事件处理程序中进行批处理。这将在未来的版本中发生变化，那么行为将更直接。但解决方案不是减少批处理，而是默认开启更多的批处理。这就是我们要做的。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/React/" class="category-chain-item">React</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>React 是否保持 state 更新的顺序？</div>
      <div>http://yan_yin.gitee.io/blog/2023/07/05/react-update/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>扬言</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年7月5日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2023/07/20/js-precision/" title="js中的精度问题之通舍通入可靠解决">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">js中的精度问题之通舍通入可靠解决</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2023/07/01/submit-form/" title="前端上传文件失败后如何再次上传流(不点击手动上传)">
                        <span class="hidden-mobile">前端上传文件失败后如何再次上传流(不点击手动上传)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="" target="_blank" rel="nofollow noopener"><span>Y</span></a> <i class="iconfont icon-love"></i> <a href="" target="_blank" rel="nofollow noopener"><span>Z</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      川ICP备19990130号-1
    </a>
  </span>
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
